package com.exam.service.serviceImpl;

import com.exam.dataobject.Examination;
import com.exam.dataobject.ExaminationResult;
import com.exam.dataobject.User;
import com.exam.dataobject.mapper.ExaminationResultMapper;
import com.exam.repository.ExaminationRepository;
import com.exam.repository.ExaminationResultRepository;
import com.exam.repository.UserRepository;
import com.exam.service.ExaminationResultService;
import com.exam.utils.RedisUserIdUtil;
import com.exam.vo.ExaminationResultVO;
import com.exam.vo.QuestionAnswerVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by v
 * Date:2019/2/23
 * Time:19:39
 */
@Service
public class ExaminationResultServiceImpl implements ExaminationResultService {

    @Autowired
    private ExaminationResultRepository examinationResultRepository;

    @Autowired
    private ExaminationRepository examinationRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ExaminationResultMapper examinationResultMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @Override
    public PageInfo<Map<String, Object>> getAllList(Integer page, Integer size) {

        PageHelper.startPage(page, size);
        Page<Map<String, Object>> all = examinationResultMapper.getExaminationResultList();
        return all.toPageInfo();
    }

    @Override
    public PageInfo<Map<String, Object>> getSelfList(HttpServletRequest request, Integer page, Integer size) {

        Integer userId = Integer.valueOf(RedisUserIdUtil.getUserId(request, redisTemplate));

        PageHelper.startPage(page, size);
        Page<Map<String, Object>> all = examinationResultMapper.getExaminationResultListByUserId(userId);
        return all.toPageInfo();
    }

    @Override
    public ExaminationResultVO getResultDetail(Integer examinationResultId) {

        ExaminationResult examinationResult = examinationResultRepository.findByExaminationResultId(examinationResultId);

        Examination examination = examinationRepository.findByExaminationId(examinationResult.getExaminationId());

        User user = userRepository.findByUserId(examinationResult.getUserId());

        ExaminationResultVO examinationResultVO = new ExaminationResultVO();
        List<QuestionAnswerVO> questionAnswerVOList = examinationResultMapper.getQuestionAnswerList(examinationResultId);

        examinationResultVO.setTitle(examination.getTitle());
        examinationResultVO.setTotalScore(examination.getTotalScore());
        examinationResultVO.setUserName(user.getUserName());
        examinationResultVO.setAnswerScore(examinationResult.getAnswerScore());
        examinationResultVO.setQuestionAnswerVOList(questionAnswerVOList);

        return examinationResultVO;
    }
}
